/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.service;

import java.io.Serializable;
import java.util.List;

import com.google.code.sapien.dao.RoleDAO;
import com.google.code.sapien.model.Role;
import com.google.code.sapien.model.User;
import com.google.inject.Inject;

/**
 * Default implementation of the {@link com.google.code.sapien.service.RoleService}.
 * @author Adam
 * @version $Id: RoleSapienService.java 30 2010-01-10 19:05:55Z a.ruggles $
 * 
 * Created on Mar 8, 2009 at 8:11:54 PM 
 */
public class RoleSapienService implements RoleService {

	/**
	 * The role data access object.
	 */
	private final RoleDAO roleDAO;

	/**
	 * Constructs a role sapien service.
	 * @param roleDAO The role data access object.
	 */
	@Inject
	public RoleSapienService(final RoleDAO roleDAO) {
		this.roleDAO = roleDAO;
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.EntityService#add(java.lang.Object,
	 *  com.google.code.sapien.security.SecurityDetails)
	 */
	public void add(final Role entity, final User creator) {
		roleDAO.add(entity);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.EntityService#get(java.io.Serializable)
	 */
	public Role get(final Serializable id) {
		return roleDAO.get(id);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.RoleService#getAll()
	 */
	public List<Role> getAll() {
		return roleDAO.listAll();
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.EntityService#remove(java.lang.Object,
	 *  com.google.code.sapien.security.SecurityDetails)
	 */
	public void remove(final Role entity, final User remover) {
		final Role role = roleDAO.load(entity.getId());
		roleDAO.remove(role);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.EntityService#update(java.lang.Object,
	 *  com.google.code.sapien.security.SecurityDetails)
	 */
	public Role update(final Role entity, final User updator) {
		final Role role = roleDAO.load(entity.getId());
		if (entity.getName() != null) {
			role.setName(entity.getName());
		}
		if (entity.getDescription() != null) {
			role.setDescription(entity.getDescription());
		}
		if (entity.getGuestDefault() != null) {
			role.setGuestDefault(entity.getGuestDefault());
		}
		if (entity.getUserDefault() != null) {
			role.setUserDefault(entity.getUserDefault());
		}
		return role;
	}
}
